//
//  SISSCoopCommentShowView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/7/28.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSCoopCommentShowView.h"
#import "SISSRateBarView.h"
#import "SISSCommentModel.h"

@interface SISSCoopCommentShowView ()

/// titleView
@property (weak, nonatomic) UIView *titleView;
/// titleLabel
@property (weak, nonatomic) UILabel *titleLabel;
/// 申诉button
@property (weak, nonatomic) UIButton *appealButton;
/// 时间Label
@property (weak, nonatomic) UILabel *timeLabel;
/// 房屋信息真实度Label
@property (weak, nonatomic) UILabel *validityLabel;
/// 服务态度满意度Label
@property (weak, nonatomic) UILabel *satisfyLabel;
/// 业务水平专业度Label
@property (weak, nonatomic) UILabel *businessLevelLabel;
/// 房屋信息真实度StarView
@property (weak, nonatomic) SISSRateBarView *validityStarView;
/// 服务态度满意度StarView
@property (weak, nonatomic) SISSRateBarView *satisfyStarView;
/// 业务水平专业度StarView
@property (weak, nonatomic) SISSRateBarView *businessLevelStarView;
/// 评语Label
@property (weak, nonatomic) UILabel *commentLabel;
/// lineView
@property (weak, nonatomic) UIView *lineView;
/// 追加时间Label
@property (weak, nonatomic) UILabel *addTimeLabel;
/// 追加内容Label
@property (weak, nonatomic) UILabel *addContentLabel;

@end
@implementation SISSCoopCommentShowView


- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self initUI];
        [self initConstraint];
    }
    
    return self;
}

- (void)setCommentModel:(SISSCommentModel *)commentModel {
    _commentModel = commentModel;
    
    self.timeLabel.text = [commentModel.insertTime substringToIndex:10];
    self.validityStarView.rate = commentModel.house_true_score;
    self.satisfyStarView.rate = commentModel.satisfaction_score;
    self.businessLevelStarView.rate = commentModel.profession_score;
    self.commentLabel.text = commentModel.content;
    
    WeakSelf
    CGFloat margin = 8.0;
    if (commentModel.lstOfAdditional.count != 0) {
        [self hideAddtionalCommentOrNoWithIsHidden:NO];
        self.addTimeLabel.text = [NSString stringWithFormat:@"%@追加",[commentModel.lstOfAdditional.firstObject.insertTime substringToIndex:10]];
        self.addContentLabel.text = commentModel.lstOfAdditional.firstObject.content;
        
        [self.addContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.addTimeLabel);
            make.top.equalTo(weakSelf.addTimeLabel.mas_bottom).offset(margin);
            make.bottom.equalTo(weakSelf);
        }];
    } else {
        [self hideAddtionalCommentOrNoWithIsHidden:YES];
        self.addTimeLabel.text = @"";
        self.addContentLabel.text =@"";
        if (commentModel) {
            [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(weakSelf.businessLevelLabel);
                make.top.equalTo(weakSelf.businessLevelLabel.mas_bottom).offset(2 * margin);
                make.right.equalTo(weakSelf).offset(-2 * margin);
                make.bottom.equalTo(weakSelf);
            }];
        }
    }
}

- (void)hideAddtionalCommentOrNoWithIsHidden:(BOOL)isHidden {
    
    self.lineView.hidden = isHidden;
    self.addTimeLabel.hidden = isHidden;
    self.addContentLabel.hidden = isHidden;
}

- (void)setTitleStr:(NSString *)titleStr {
    _titleStr = titleStr;
    
    self.titleLabel.text = titleStr;
}

- (void)setIsShowRightButton:(BOOL)isShowRightButton {
    _isShowRightButton = isShowRightButton;
    self.appealButton.hidden = !isShowRightButton;
}


/**
 申诉按钮点击事件
 */
- (void)appealButtonClickAction {
    
    if ([self.delegate respondsToSelector:@selector(rightAppealButtonClickAction)]) {
        [self.delegate rightAppealButtonClickAction];
    }
}

#pragma mark - UI
- (void)initUI {
    
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:titleView];
    self.titleView = titleView;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = MAIN_TEXT_COLOR;
    titleLabel.text = @"我对他的评价";
    [self.titleView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UIButton *appealButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [appealButton setTitle:@"申诉" forState:UIControlStateNormal];
    [appealButton setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
    [appealButton addTarget:self action:@selector(appealButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    appealButton.hidden = !self.isShowRightButton;
    [self.titleView addSubview:appealButton];
    self.appealButton = appealButton;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = [UIFont systemFontOfSize:16];
    timeLabel.textColor = MAIN_TEXT_COLOR;
    timeLabel.text = @"2017-7-28";
    [self addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    UILabel *validityLabel = [[UILabel alloc] init];
    validityLabel.font = [UIFont systemFontOfSize:16];
    validityLabel.textColor = MAIN_TEXT_COLOR;
    validityLabel.text = @"房屋信息真实度：";
    [self addSubview:validityLabel];
    self.validityLabel = validityLabel;
    
    SISSRateBarView *validityView = [[SISSRateBarView alloc] init];
    validityView.starTintColor = [UIColor redColor];
    [self addSubview:validityView];
    self.validityStarView = validityView;
    
    UILabel *satisfyLabel = [[UILabel alloc] init];
    satisfyLabel.font = [UIFont systemFontOfSize:16];
    satisfyLabel.textColor = MAIN_TEXT_COLOR;
    satisfyLabel.text = @"服务态度满意度：";
    [self addSubview:satisfyLabel];
    self.satisfyLabel = satisfyLabel;
    
    SISSRateBarView *satisfyStarView = [[SISSRateBarView alloc] init];
    satisfyStarView.starTintColor = [UIColor redColor];
    [self addSubview:satisfyStarView];
    self.satisfyStarView = satisfyStarView;
    
    UILabel *businessLevelLabel = [[UILabel alloc] init];
    businessLevelLabel.font = [UIFont systemFontOfSize:16];
    businessLevelLabel.textColor = MAIN_TEXT_COLOR;
    businessLevelLabel.text = @"业务水平专业度：";
    [self addSubview:businessLevelLabel];
    self.businessLevelLabel = businessLevelLabel;
    
    SISSRateBarView *businessLevelStarView = [[SISSRateBarView alloc] init];
    businessLevelStarView.starTintColor = [UIColor redColor];
    [self addSubview:businessLevelStarView];
    self.businessLevelStarView = businessLevelStarView;
    
    UILabel *commentLabel = [[UILabel alloc] init];
    commentLabel.font = [UIFont systemFontOfSize:16];
    commentLabel.textColor = MAIN_TEXT_COLOR;
    commentLabel.numberOfLines = 0;
    commentLabel.text = @"评语：";
    [self addSubview:commentLabel];
    self.commentLabel = commentLabel;

    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:lineView];
    self.lineView = lineView;
    
    UILabel *addTimeLabel = [[UILabel alloc] init];
    addTimeLabel.font = [UIFont systemFontOfSize:16];
    addTimeLabel.textColor = MAIN_TEXT_COLOR;
    addTimeLabel.text = @"2017-7-28追加";
    [self addSubview:addTimeLabel];
    self.addTimeLabel = addTimeLabel;
    
    UILabel *addContentLabel = [[UILabel alloc] init];
    addContentLabel.font = [UIFont systemFontOfSize:16];
    addContentLabel.textColor = MAIN_TEXT_COLOR;
    addContentLabel.text = @"追加内容";
    addContentLabel.numberOfLines = 0;
    [self addSubview:addContentLabel];
    self.addContentLabel = addContentLabel;
}

#pragma mark - 约束
- (void)initConstraint {
    
    CGFloat margin = 8.0;
    WeakSelf
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf);
        make.top.equalTo(weakSelf).offset(margin);
        make.height.mas_equalTo(5 * margin);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleView).offset(2 * margin);
        make.centerY.equalTo(weakSelf.titleView);
    }];
    
    [self.appealButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.titleView).offset(-margin);
        make.centerY.equalTo(weakSelf.titleView);
    }];
    
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(2 * margin);
        make.top.equalTo(weakSelf.titleView.mas_bottom).offset(margin);
    }];
    
    [self.validityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.timeLabel.mas_bottom).offset(margin);
        make.left.equalTo(weakSelf.timeLabel);
    }];
    
    [self.validityStarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.validityLabel.mas_right).offset(margin);
        make.centerY.equalTo(weakSelf.validityLabel);
        make.size.mas_equalTo(CGSizeMake(200, 30));
    }];
    
    [self.satisfyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.validityLabel);
        make.top.equalTo(weakSelf.validityLabel.mas_bottom).offset(margin);
    }];
    
    [self.satisfyStarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.validityStarView);
        make.centerY.equalTo(weakSelf.satisfyLabel);
        make.size.equalTo(weakSelf.validityStarView);
    }];
    
    [self.businessLevelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.satisfyLabel);
        make.top.equalTo(weakSelf.satisfyLabel.mas_bottom).offset(margin);
    }];
    
    [self.businessLevelStarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.satisfyStarView);
        make.centerY.equalTo(weakSelf.businessLevelLabel);
        make.size.equalTo(weakSelf.satisfyStarView);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.businessLevelLabel);
        make.top.equalTo(weakSelf.businessLevelLabel.mas_bottom).offset(2 * margin);
        make.right.equalTo(weakSelf).offset(-2 * margin);
    }];

    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(margin);
        make.right.equalTo(weakSelf).offset(-margin);
        make.top.equalTo(weakSelf.commentLabel.mas_bottom).offset(margin);
        make.height.mas_equalTo(1);
    }];
    
    [self.addTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.commentLabel);
        make.top.equalTo(weakSelf.lineView.mas_bottom).offset(margin);
    }];
    
    [self.addContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.addTimeLabel);
        make.top.equalTo(weakSelf.addTimeLabel.mas_bottom).offset(margin);
        make.right.lessThanOrEqualTo(weakSelf).offset(-2 * margin);
//        make.bottom.equalTo(weakSelf);
    }];
}

@end
